package com.sk.skplanet.commute.controllers;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.actions.Actions;
import com.sk.skplanet.commute.views.PointList;
import com.sk.skplanet.commute.views.RouteList;
import com.sk.skplanet.commute.vo.Data;
import com.sk.skplanet.commute.vo.Route;

public class RouteController {
	/**
	 * 북마크 항목 내 내부 위치, 버스정보를 담은 레이아웃 생성 메소드
	 * 
	 * @param : 북마크 액티비티의 컨텍스트
	 * @return : 내부 위치, 버스정보를 담은 레이아웃
	 */
	public static LinearLayout makeRoute(final Context context, final int routePosition) {
		Route route = Data.memberData.getRoutes().get(routePosition);
		final Route routeToPass = route;
		LinearLayout layout_route = new LinearLayout(context);
		layout_route.setOrientation(LinearLayout.HORIZONTAL);
		// layout_route.setBackgroundColor(Color.parseColor(Data.layout_routeColor));
		layout_route.setBackgroundResource(R.drawable.category_menu_bg_nor);

		LayoutParams layoutParams_layout_route = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				Actions.DPFromPixel(150));
		layoutParams_layout_route.setMargins(0, Actions.DPFromPixel(5), 0, Actions.DPFromPixel(5));
		layout_route.setLayoutParams(layoutParams_layout_route);
		layout_route.setPadding(Actions.DPFromPixel(30), 0, 0, 0);
		layout_route.setOnTouchListener(new OnTouchListener() { // 버튼 터치시 이벤트
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) // 버튼을 누르고 있을 때
						v.setBackgroundResource(R.drawable.category_menu_bg_sel);
						v.setPadding(Actions.DPFromPixel(30), 0, 0, 0);
						if (event.getAction() == MotionEvent.ACTION_MOVE) { // 버튼에서 손을 떼었을 때
							if (!v.isPressed() || !v.isSelected()) {
								v.setBackgroundResource(R.drawable.category_menu_bg_nor);
								v.setPadding(Actions.DPFromPixel(30), 0, 0, 0);
							}
						}
						return false;
					}
				});
		layout_route.setId((int) route.getId());
		layout_route.setTag(route);

		TextView textview_route = new TextView(context);
		textview_route.setText(route.getName());
		textview_route.setTextColor(Color.parseColor(Data.textview_routeColor));
		textview_route.setTextSize((float) Actions.DPFromPixel(20));

		textview_route.setGravity(Gravity.CENTER_VERTICAL);
		android.view.ViewGroup.LayoutParams layoutParams_textview_route = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		textview_route.setLayoutParams(layoutParams_textview_route);

		layout_route.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intentRouteMenu = new Intent(context, PointList.class);
				intentRouteMenu.putExtra("routePosition", routePosition);
				context.startActivity(intentRouteMenu);
				((Activity) context).finish();
			}
		});

		layout_route.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				DialogController.createDeleteDialog(context, routeToPass, v).show();
				return true;
			}
		});

		layout_route.addView(textview_route);

		return layout_route;
	}
}
